package com.wudl.db.service;
import com.wudl.db.config.MyThreadLocal;
import com.wudl.db.model.User;
import com.wudl.db.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName : UserService
 * @Description :
 * @Author :wudl
 * @Date: 2021-08-06 00:05
 */

@Service("userService")
public class UserService {
    @Resource
    private UserMapper userMapper;



    public void add(User user){
        //切换数据源为主库
        MyThreadLocal.setDataSource("master");
        userMapper.insert(user);
    }
    public List<User> selectAllMaster(){
        //切换数据源为从库
        MyThreadLocal.setDataSource("slave");
        return  userMapper.selectAll();
    }
    public List<User> selectAllSlave(){
        //切换数据源为从库
        MyThreadLocal.setDataSource("master");
        return  userMapper.selectAll();
    }
}
